<?php
class autokeyword {

	
	var $contents;
	var $idlang;
	var $keywords;
	var $wordLengthMin;
	var $wordOccuredMin;
	var $word2WordPhraseLengthMin;
	var $phrase2WordLengthMinOccur;
	var $word3WordPhraseLengthMin;
	var $phrase2WordLengthMin;
	var $phrase3WordLengthMinOccur;
	var $phrase3WordLengthMin;

	function autokeyword($contenuto, $idlang='IT')
	{
		//get parameters
		$this->idlang = $idlang;		
		$this->contents = utf8_decode($contenuto);
        
        $this->contents = $this->replace_chars($this->contents);
        
		// single word
		$this->wordLengthMin = 4;
		$this->wordOccuredMin = 1;

		// 2 word phrase
		$this->word2WordPhraseLengthMin = 3;
		$this->phrase2WordLengthMin = 10;
		$this->phrase2WordLengthMinOccur = 3;

		// 3 word phrase
		$this->word3WordPhraseLengthMin = 4;
		$this->phrase3WordLengthMin = 10;
		$this->phrase3WordLengthMinOccur = 2;

	}

	function get_keywords()
	{
		$keywords = $this->parse_words();
		//$this->parse_2words().
		return substr($keywords, 0, -2);
	}

	//turn the site contents into an array
	//then replace common html tags.
	function replace_chars($content)
	{
		//convert all characters to lower case
		$content = strtolower($content);
		
		$content = strip_tags($content);

		$punctuations = array(',', ')', '(', '.', "'", '"',
		'<', '>', '!', '?', '/', '-',
		'_', '[', ']', ':', '+', '=', '#',
		'$', '&quot;', '&copy;', '&gt;', '&lt;', '&nbsp;', '&quot;',
		chr(10), chr(13), chr(9),'<div>','</div>');

		$content = str_replace($punctuations, " ", $content);
		// replace multiple gaps
		$content = preg_replace('/ {2,}/si', " ", $content);

		return $content;
	}

	//single words META KEYWORDS
	function parse_words()
	{
		//list of commonly used words
		// this can be edited to suit your needs        
        $common["IT"] = array("&agrave;", "&egrave;", "&igrave;", "&ograve;", "&ugrave;","abbiamo","capace","circa","sopra","fatto","ancora","contro","accordo","delle","della","dello","questa","questo","questi","del","aveva","sulla","sulle","stata","dalla","detto","prima","tutti","tutto","solo","dopo","contro","quasi","solo","anche","altro","altri","altre","invece","br", "img", "p","lt", "gt", "quot", "copy","afferma","vicenda","degli","nelle","siamo","sono","siete","parte","seguito","perch�","perch�","aggiunge","forse","andare","sempre","oggi","stamani","aprendo","appena","penso","nessun","&quot","&nbsp","con","continua","qui","qua","pi&ugrave;","&amp;");
		//create an array out of the site contents
		$s = split(" ", $this->contents);
		//initialize array
		$k = array();
		//iterate inside the array
		foreach( $s as $key=>$val ) {
			//delete single or two letter words and
			//Add it to the list if the word is not
			//contained in the common words list.
			if(strlen(trim($val)) >= $this->wordLengthMin  && !in_array(trim($val), $common[$this->idlang])  && !is_numeric(trim($val))) {
				$k[] = trim($val);
			}
		}
		//count the words
		$k = array_count_values($k);
		//sort the words from
		//highest count to the
		//lowest.
		$occur_filtered = $this->occure_filter($k, $this->wordOccuredMin);
		arsort($occur_filtered);

		$imploded = $this->implode(", ", $occur_filtered);
		//release unused variables
		unset($k);
		unset($s);

		return $imploded;
	}

	function parse_2words()
	{
		//create an array out of the site contents
		$x = split(" ", $this->contents);
		//initilize array

		//$y = array();
		for ($i=0; $i < count($x)-1; $i++) {
			//delete phrases lesser than 5 characters
			if( (strlen(trim($x[$i])) >= $this->word2WordPhraseLengthMin ) && (strlen(trim($x[$i+1])) >= $this->word2WordPhraseLengthMin) )
			{
				$y[] = trim($x[$i])." ".trim($x[$i+1]);
			}
		}

		//count the 2 word phrases
        if ($y) $y = array_count_values($y);
 
		$occur_filtered = $this->occure_filter($y, $this->phrase2WordLengthMinOccur);
		//sort the words from highest count to the lowest.
		arsort($occur_filtered);

		$imploded = $this->implode(", ", $occur_filtered);
		//release unused variables
		unset($y);
		unset($x);

		return $imploded;
	}

	function parse_3words()
	{
		//create an array out of the site contents
		$a = split(" ", $this->contents);
		//initilize array
		$b = array();

		for ($i=0; $i < count($a)-2; $i++) {
			//delete phrases lesser than 5 characters
			if( (strlen(trim($a[$i])) >= $this->word3WordPhraseLengthMin) && (strlen(trim($a[$i+1])) > $this->word3WordPhraseLengthMin) && (strlen(trim($a[$i+2])) > $this->word3WordPhraseLengthMin) && (strlen(trim($a[$i]).trim($a[$i+1]).trim($a[$i+2])) > $this->phrase3WordLengthMin) )
			{
				$b[] = trim($a[$i])." ".trim($a[$i+1])." ".trim($a[$i+2]);
			}
		}

		//count the 3 word phrases
		$b = array_count_values($b);
		//sort the words from
		//highest count to the
		//lowest.
		$occur_filtered = $this->occure_filter($b, $this->phrase3WordLengthMinOccur);
		arsort($occur_filtered);

		$imploded = $this->implode(", ", $occur_filtered);
		//release unused variables
		unset($a);
		unset($b);

		return $imploded;
	}

	function occure_filter($array_count_values, $min_occur)
	{
		$occur_filtered = array();
        if (count($array_count_values)) {
    		foreach ($array_count_values as $word => $occured) {
    			if ($occured >= $min_occur) {
    				$occur_filtered[$word] = $occured;
    			}
    		}
        }
		return $occur_filtered;
	}

	function implode($gule, $array)
	{
		$c = "";
		foreach($array as $key=>$val) {
			@$c .= $key.$gule;
		}
		return $c;
	}
}
?>
